package hbase.DemoTest;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;


public class ZB_Data {
    private static String tableName = "food";
    private static String sexList[] = {"男","女"};

    // 添加数据不需要Admin
    public static void main(String[] args) throws IOException {
        Configuration conf = HBaseConfiguration.create();
        Connection HConn = null;
        Table table = null;

        try {
            HConn = ConnectionFactory.createConnection(conf);
            table = HConn.getTable(TableName.valueOf(tableName));

            // 创建Put列表存放多条数据
            List<Put> puts = new ArrayList<>();

            int batchSize = 10000;
            for (int i = 0; i < 1000000; i++) {
                // 插入数据 shell => put “表名”，“行建”，“列柱：列名”，“值”
                Put put1 = new Put(String.valueOf(i).getBytes()); // put “表名”，“行建”
                String name = person.generateRandomName();
                put1.addColumn("info".getBytes(),"name".getBytes(),name.getBytes());

                Put put2 = new Put(String.valueOf(i).getBytes());
                Random random = new Random();
                String sex = sexList[random.nextInt(2)];
                put2.addColumn("info".getBytes(),"sex".getBytes(),sex.getBytes());

                Put put3 = new Put(String.valueOf(i).getBytes());
                String age = String.valueOf(person.generateRandomAge());
                put3.addColumn("info".getBytes(),"age".getBytes(),age.getBytes());

                Put put4 = new Put(String.valueOf(i).getBytes());
                String address = person.generateRandomCityWithProvince();
                put4.addColumn("info".getBytes(),"address".getBytes(),address.getBytes());

                puts.add(put1);
                puts.add(put2);
                puts.add(put3);
                puts.add(put4);

                if ((i + 1) % batchSize == 0) {
                    try {
                        table.put(puts);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    puts.clear();
                }
            }

            if (!puts.isEmpty()) {
                try {
                    table.put(puts);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

//            // 提交列表集合数据
//            try {
//                table.put(puts);
//            } catch (IOException e) {
//                // 在这里处理提交数据时的异常
//                e.printStackTrace();
//                // 可以根据具体需求进行重试或其他处理
//            }

        } catch (IOException e) {
            // 处理获取连接、获取表等操作时的异常
            e.printStackTrace();
        } finally {
            // 释放资源
            if (table!= null) {
                try {
                    table.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (HConn!= null) {
                try {
                    HConn.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("ok-----------------");
    }
}